<?php


namespace api\modules\v1\controllers\buss;

use common\models\base\CommonStatus;
use common\models\base\Provinces;
use services\base\CommonStatusService;
use services\base\ProvincesService;
use services\buss\AccountsConsumeService;
use services\buss\AccountsFansService;
use services\buss\DevicesService;
use services\buss\DoctorsService;
use services\buss\PlatformService;
use services\member\MemberService;
use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\buss\AccountsService;

class AccountsConsumeController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/accounts-consume/index 投流消费列表
     * @apiGroup AccountsConsume
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} date_stime                开始时间
     * @apiParam {String} date_etime                结束时间
     * @apiParam {String} keywords                  搜索词
     * @apiParam {int} project_id                   项目ID
     * @apiParam {int} platform_id                  平台ID
     * @apiParam {int} use_mid                      运营人ID
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {Number}  data.list.account_id                   账号ID
     * @apiSuccess {String}  data.list.money                        金额
     * @apiSuccess {Number}  data.list.date_time_str                时间
     * @apiSuccess {Number}  data.list.city_name                    投放地区
     * @apiSuccess {Number}  data.list.hours                        时长
     * @apiSuccess {Number}  data.list.type_str                     投放类型
     * @apiSuccess {String}  data.list.hour_minute                  投放时间
     * @apiSuccess {String}  data.list.private_sms_num              有效私信
     * @apiSuccess {String}  data.list.effective_clue_num           有效线索
     * @apiSuccess {String}  data.list.give_mobile_num              留电数
     * @apiSuccess {String}  data.list.operation_name               操作人
     * @apiSuccess {String}  data.list.platform_name                平台
     * @apiSuccess {String}  data.list.project_name                 项目
     * @apiSuccess {String}  data.list.department_name              部门
     * @apiSuccess {String}  data.list.use_mname                    使用人
     * @apiSuccess {String}  data.list.account_status               账号状态
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {Object}  condition
     * @apiSuccess {Object}  condition.type                             投放类型
     * @apiSuccess {Object}  condition.city                             城市
     * @apiSuccess {String}  data.condition.principal_member_arr        运营人筛选
     * @apiSuccess {String}  data.condition.project_arr                 项目筛选【{value:'', label:''}】
     * @apiSuccess {String}  data.condition.platform_arr                平台筛选
     *
     */
    public function actionIndex(): array
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $service = new AccountsConsumeService();
        $list = $service->consumeList($params, $page, $pageSize);
        $provinceService = new ProvincesService();
        $list['condition'] = [
            'type' => $service->getTypesCondition(),
            'project_arr' => MemberService::getProjectListCondition(),
            'platform_arr' => PlatformService::getListCondition(),
            'principal_member_arr' => AccountsService::getPrincipalMemberCondition(),
            'city' => $provinceService->getCityCondition(),
            'account_arr' => $service->getEditCondition(1),
            'department_arr' => MemberService::getMemberDepartmentCondition()['list'],
        ];
        return $list;
    }

    /**
     * @api {post} /v1/buss/accounts-consume/create 新增投流消费记录
     * @apiGroup AccountsConsume
     * @apiName create
     * @apiUse CommonParam
     *
     * @apiBody {Number} account_id                 账号ID
     * @apiBody {Number} money                      消费金额
     * @apiBody {String} date_time                  投流日期
     * @apiBody {String} city_id                    地区ID
     * @apiBody {String} hours                      时长（小时数）
     * @apiBody {String} type                       投放类型【1：涨粉数、2：主页浏览数 3：点赞评论 4：播放量】
     * @apiBody {String} hour_minute                投流时间【时分】
     * @apiBody {Number} is_master                  达人【1是、0否】
     * @apiBody {String} remark                     备注
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['account_id']) || !$params['account_id']) {
            return CommonHelper::api(422, '账号ID必传');
        }
        if (!isset($params['date_time']) || !$params['date_time']) {
            return CommonHelper::api(422, '消费时间必填');
        }
        if (!isset($params['money']) || !$params['money']) {
            return CommonHelper::api(422, '消费金额必填');
        }
        if (!isset($params['city_id']) || !$params['city_id']) {
            return CommonHelper::api(422, '投放地区必填');
        }
        if (!isset($params['hours']) || !$params['hours']) {
            return CommonHelper::api(422, '时长不能为空');
        }
        if (!isset($params['type']) || !$params['type']) {
            return CommonHelper::api(422, '投放类型不能为空');
        }
        if (!isset($params['hour_minute']) || !$params['hour_minute']) {
            return CommonHelper::api(422, '投放时间不能为空');
        }

        $service = new AccountsConsumeService();
        return $service->createConsume($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/accounts-consume/update 编辑投流消费记录
     * @apiGroup AccountsConsume
     * @apiName update
     * @apiUse CommonParam
     *
     * @apiBody {Number} account_id             账号ID
     * @apiBody {Number} money                  消费金额
     * @apiBody {String} date_time              消费时间
     * @apiBody {String} city_id                地区ID
     * @apiBody {String} hours                  时长（小时数）
     * @apiBody {String} type                   投放类型【1：涨粉数 2：主页浏览数 3：点赞评论 4：播放量】
     * @apiBody {String} hour_minute            投流时间【时分】
     * @apiBody {Number} is_master                  达人【1是、0否】
     * @apiBody {String} remark                 备注
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '编辑时ID必传');
        }
        if (!isset($params['account_id']) || !$params['account_id']) {
            return CommonHelper::api(422, '账号ID必传');
        }
        if (!isset($params['date_time']) || !$params['date_time']) {
            return CommonHelper::api(422, '消费时间必填');
        }
        if (!isset($params['money']) || !$params['money']) {
            return CommonHelper::api(422, '消费金额必填');
        }
        if (!isset($params['city_id']) || !$params['city_id']) {
            return CommonHelper::api(422, '地区必填');
        }
        if (!isset($params['hours']) || !$params['hours']) {
            return CommonHelper::api(422, '时长必填');
        }
        if (!isset($params['hour_minute']) || !$params['hour_minute']) {
            return CommonHelper::api(422, '投流时间必填');
        }

        $service = new AccountsConsumeService();
        return $service->updateConsume($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/accounts-consume/update-data 编辑投流消费记录数据
     * @apiGroup AccountsConsume
     * @apiName update-data
     * @apiUse CommonParam
     *
     * @apiBody {Number} consume_id             消费ID
     * @apiBody {Number} private_sms_num        有效私信
     * @apiBody {Number} effective_clue_num     有效线索
     * @apiBody {Number} give_mobile_num        留电数
     * @apiBody {Number} recommend_num          推荐数
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionUpdateData()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['consume_id']) || !$params['consume_id']) {
            return CommonHelper::api(422, '消费ID必传');
        }

        $service = new AccountsConsumeService();
        return $service->updateConsumeData($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/accounts-consume/delete 删除消费
     * @apiGroup AccountsConsume
     * @apiName delete
     * @apiUse CommonParam
     *
     * @apiBody {Number} consume_id     消费ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDelete()
    {
        $consumeId = Yii::$app->request->post('consume_id');
        if (!$consumeId) {
            return CommonHelper::api(422, '缺少消费ID');
        }

        $service = new AccountsConsumeService();
        return $service->deleteConsume($consumeId);
    }
}
